package com.example.todo.Activities;

import java.util.List;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

import com.example.todo.ItemsAdapter;
import com.example.todo.R;
import com.example.todo.DAL.IItemRepository;
import com.example.todo.DAL.ItemRepository;
import com.example.todo.Entities.Item;

public class MainActivity extends Activity {

	private ListView _lvItems;
	private ItemsAdapter _adapter;
	private IItemRepository _itemRepository;
	private List<Item> _items;
	private static final int SUBACTIVITY_EditItem = 1;

	// Events
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ActionBar actionBar = getActionBar();
		actionBar.setHomeButtonEnabled(true);
		_lvItems = (ListView) findViewById(R.id.lvItems);

		_itemRepository = new ItemRepository(getApplicationContext());

		LoadData();
		InitEventHandlers();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case (SUBACTIVITY_EditItem):
			if (resultCode == Activity.RESULT_OK)
				LoadData();
			break;

		default:
			break;
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.action_delete:
			DeleteCheckedItems();
			LoadData();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	// Private Methods
	private void LoadData() {
		_items = _itemRepository.GetAll();
		_adapter = new ItemsAdapter(this, _items);
		_lvItems.setAdapter(_adapter);
	}

	private void DeleteCheckedItems() {
		List<Item> checkedItems = _adapter.GetSelectedItems();

		for (Item item : checkedItems) {
			_itemRepository.Delete(item);
		}
	}

	private void StartEditActivity(Item item) {
		Intent intent = new Intent(MainActivity.this, EditItemActivity.class);
		intent.putExtra("item", item);
		startActivityForResult(intent, SUBACTIVITY_EditItem);
	}

	private void InitEventHandlers() {
		final Button btnAdd = (Button) findViewById(R.id.btnAdd);
		btnAdd.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				StartEditActivity(new Item());
			}
		});

		_lvItems.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				StartEditActivity(_items.get(position));
			}
		});
	}
}
